package com.wheesper.www.WheesperServer.Services;

import com.Wheesper.www.WheesperServer.Model.UserCenter;
import com.Wheesper.www.WheesperServer.Model.UserInfo;
import com.Wheesper.www.WheesperServer.Model.UserInfoCenter;
import com.wheesper.www.WheesperServer.Net.Session.IOSession;

import ProtocolBuffer.ProtoMessageOuterClass.ProtoMessage;
import ProtocolBuffer.ProtoMessageOuterClass.UserInfoModifyRequest;
import ProtocolBuffer.ProtoMessageOuterClass.UserInfoModifyResponse;
import ProtocolBuffer.ProtoMessageOuterClass.UserInfoQueryResponse;

public class UserInfoController {
	private static UserInfoController _instance = new UserInfoController();
	public static UserInfoController Instance() {
		return _instance;
	}
	
	private void queryResponse(IOSession session,UserInfo info,String email) {
		UserInfoQueryResponse response = UserInfoQueryResponse.newBuilder()
				.setAge(info.Age())
				.setCity(info.City())
				.setCountry(info.Country())
				.setProvince(info.Province())
				.setMailAddress(email)
				.setNickname(info.NickName())
				.setSex(info.Sex())
				.setCreateDate(info.CreatedDate().toString())
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setUserInfoQueryResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());		
	}
	
	private void modifyResponse(IOSession session,String email) {
		UserInfoModifyResponse response = UserInfoModifyResponse.newBuilder()
				.setMailAddress(email)
				.build();
		ProtoMessage message = ProtoMessage.newBuilder()
				.setUserInfoModifyResponse(response)
				.build();
		session.writeToRemote(message.toByteArray());
	}
	
	
	public void modifying(IOSession session,UserInfoModifyRequest request) {
		String email = request.getMailAddress();
		Integer ID = UserCenter.Instance().UserObject(email).ID();
		String nickname = request.getNickname();
		String sex = request.getSex();
		Integer age = request.getAge();
		String country = request.getCountry();
		String province = request.getProvince();
		String city = request.getCity();
		UserInfo info = new UserInfo(ID);
		info.Age(age);
		info.NickName(nickname);
		info.Sex(sex);
		info.City(city);
		info.Country(country);
		info.Province(province);
		UserInfoCenter.Instance().modify(info);
		modifyResponse(session,email);
	}
	
	public void querying(IOSession session,String email) {
		UserInfo info = UserInfoCenter.Instance().UserInfo(email);
		queryResponse(session,info,email);
	}
}
